package com.wss.lsl.lucene.demo.shipin.advanced.query;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.NumericRangeFilter;
import org.apache.lucene.search.QueryWrapperFilter;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TermRangeFilter;
import org.apache.lucene.search.WildcardQuery;
import org.junit.Test;

public class SearchTest {

	@Test
	public void index() {
		FileIndexUtils.index(true);
	}

	@Test
	public void test01() {
		// 默认通过评分排序
		System.out.println("\n\n--------------null------------------");
		SearchUtils.searchBySort("href", null);

		// 评分排序，不输出评分
		System.out
				.println("\n\n--------------Sort.RELEVANCE------------------");
		SearchUtils.searchBySort("href", Sort.RELEVANCE);

		// 根据doc id排序
		System.out
				.println("\n\n--------------Sort.INDEXORDER------------------");
		SearchUtils.searchBySort("href", Sort.INDEXORDER);

		// 根据特定地段排序
		System.out.println("\n\n--------------通过文件大小排序------------------");
		SearchUtils.searchBySort("href", new Sort(new SortField("size",
				SortField.LONG)));

		System.out.println("\n\n--------------通过文件名排序------------------");
		SearchUtils.searchBySort("href", new Sort(new SortField("filename",
				SortField.STRING)));

		System.out.println("\n\n--------------通过文件名排序(降序)------------------");
		SearchUtils.searchBySort("href", new Sort(new SortField("filename",
				SortField.STRING, true)));

		System.out
				.println("\n\n--------------通过大小、文件名排序(多字段排序)------------------");
		SearchUtils.searchBySort("href", new Sort(new SortField("size",
				SortField.LONG), new SortField("filename", SortField.STRING)));
	}

	@Test
	public void test02() {
		System.out.println("\n\n--------------null------------------");
		SearchUtils.searchByFilter("href", null);

		Filter filter = new TermRangeFilter("filename",
				"allclasses-frame.html", "deprecated-list.html", true, true);
		System.out
				.println("\n\n--------------TermRangeFilter------------------");
		SearchUtils.searchByFilter("href", filter);

		filter = NumericRangeFilter.newLongRange("size", 1000l, 4000l, true,
				true);
		System.out
				.println("\n\n--------------NumericRangeFilter------------------");
		SearchUtils.searchByFilter("href", filter);

		filter = new QueryWrapperFilter(new WildcardQuery(new Term("filename",
				"*.html")));
		System.out
				.println("\n\n--------------QueryWrapperFilter------------------");
		SearchUtils.searchByFilter("href", filter);

	}

	// 自定义评分规则CustomScoreQuery、CustomScoreProvider
	@Test
	public void test03() {
		SearchUtils.searchByScoreQuery("href");
	}

	@Test
	public void test04() {
		SearchUtils.searchByFilenameScoreQuery("href");
	}

	@Test
	public void test05() {
		SearchUtils.searchByDateScoreQuery("href");
	}

	// 自定义parse
	@Test
	public void test06() {
		SearchUtils.searchByCustomParse("filename:[a TO d]");
		System.out.println("\n\n");
		SearchUtils.searchByCustomParse("href");

		try {
			System.out.println("\n\n");
			SearchUtils.searchByCustomParse("href~");
		} catch (Exception e) {
			e.printStackTrace();
		}

		SearchUtils.searchByCustomParse("size:[1000 TO 3000]");
	}

	// 自定义filter
	@Test
	public void test07() {
//		System.out.println("\n-----------------null-------------------");
//		SearchUtils.searchByCustomFilter("href", null);
//		System.out.println("\n-----------------MyIDFilter-------------------");
//		SearchUtils.searchByCustomFilter("href", new MyIDFilter());
		final String[] delIds = new String[] { "0", "3", "5", "6", "7" };
		//自定义filter的合理设计
		System.out.println("\n-----------------null-------------------");
		SearchUtils.searchByCustomFilter("href", null);
		System.out.println("\n-----------------MyIDFilter-------------------");
		SearchUtils.searchByCustomFilter("href", new MyIDFilter(new FilterAccessor() {
			
			@Override
			public String[] values() {
				return delIds;
			}
			
			@Override
			public boolean set() {
				return true;
			}
			
			@Override
			public String getField() {
				return "id";
			}
		}));
	}

}
